class Vertigo extends AcGameObject{
    constructor(playground, player , x ,y ,radius, tx ,ty , time , damage , move_length, color , speed){
        super();
        this.playground = playground;
        this.ctx = this.playground.game_map.ctx;
        this.x = x;
        this.y = y;
        this.tx = tx;
        this.ty = ty;
        this.time = time;
        this.player = player;
        this.damage = damage;
        this.move_length = move_length;
        this.speed = speed;
        this.color = color;
        this.radius = radius;

        this.eps = 0.01;
    }

    start(){
    }


    update(){
        if(this.move_length < this.eps){
            this.distory();
            return false;
        }
        this.update_move();

        if(this.player.character !== "enemy"){
            this.update_vertigo();
        }
        this.render();
    }

    update_move(){
        let moved = Math.min(this.move_length, this.speed * this.timedelta / 1000);
        this.x += this.tx * moved;
        this.y += this.ty * moved;
        this.move_length -= moved;
    }

    update_vertigo(){
        for(var i = 0 ; i < this.playground.players.length ; i++){
            var player = this.playground.players[i];
            if(this.player !== player && this.is_collision(player)){
                this.vertigo(player);
                break;
            }
        }
    }

    get_dist(x1,y1,x2,y2){
        var dx = x1 - x2;
        var dy = y1 - y2;
        return Math.sqrt(dx * dx + dy * dy);
    }

    is_collision(player){
        let distance = this.get_dist(this.x , this.y , player.x , player.y);
        if(distance < this.radius + player.radius){
            return true;
        }

        return false;
    }

    vertigo(player){
        let angle = Math.atan2(player.y - this.y , player.x - this.x);
        player.is_attacked(angle , this.damage , 2 , this.time);
        if(this.playground.mode === "multi mode"){
            this.playground.mps.send_vertigo(player.uuid,player.x,player.y,angle,this.time, this.damage , this.uuid);
        }
        this.distory();
    }


    render(){
        var scale = this.playground.scale;
        this.ctx.beginPath();
        this.ctx.arc(this.x * scale , this.y * scale , this.radius * scale , 0 , Math.PI * 2 ,false);
        this.ctx.fillStyle = this.color;
        this.ctx.fill();
    }
}
